package daily_exercise;

/**
 * @Author self
 * @Date 2025/7/10 20:18
 * @Describe ThreadLocal应用案例
 */
public class ThreadIdExample {
    // 创建一个ThreadLocal变量，用于存储每个线程的唯一ID
    private static final ThreadLocal<Integer> threadId = new ThreadLocal<>();

    public static void main(String[] args) throws InterruptedException {
        Runnable task = () -> {
            // 初始化每个线程的唯一ID
            if (threadId.get() == null) {
                threadId.set((int)(Math.random() * 100));
            }
            System.out.println("Thread ID: " + threadId.get());
        };

        Thread t1 = new Thread(task);
        Thread t2 = new Thread(task);

        t1.start();
        t2.start();

        t1.join();
        t2.join();
    }
}
